package bookstore.dao;

import bookstore.idao.IAdminDao;
import bookstore.model.Admin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class AdminDao implements IAdminDao {
    @Override
    public Admin find(String name, String password) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;
        try {
            con = MySQLHelper.connect();
            String sql = "select * from admin where name=? and password=?";
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setName(rs.getString("name"));
                admin.setPassword(rs.getString("password"));
                admin.setRealName(rs.getString("realname"));
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return admin;
    }
    @Override
    public void updatePassword(Admin admin){
        
    }
     @Override
    public void updateMessage(Admin admin){
        
    }
    @Override
    public void update(Admin admin) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "update admin set name=?,password=?,realname=? where id=?";
            ps = con.prepareStatement(sql);
            ps.setString(1, admin.getPassword());
            ps.setString(2, admin.getRealName());
            ps.setString(3, admin.getName());
            ps.setInt(4,admin.getId());
            ps.executeUpdate();
        } catch (Exception e) {
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }

    @Override
    public void insert(Admin admin) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "insert into admin(name,password,realname) values(?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, admin.getName());
            ps.setString(2, admin.getPassword());
            ps.setString(3, admin.getRealName());
            ps.executeUpdate();
        } catch (Exception e) {
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }

    @Override
    public void delete(Integer id) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "delete from admin where id=?";
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }

    @Override
    public Admin findById(Integer id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;
        try {
            con = MySQLHelper.connect();
            String sql = "select * from admin where id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setName(rs.getString("name"));
                admin.setPassword(rs.getString("password"));
                admin.setRealName(rs.getString("realname"));
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return admin;
    }

    @Override
    public List<Admin> findAll() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Admin> list = new ArrayList<Admin>();
        try {
            con = MySQLHelper.connect();
            String sql = "select * from admin";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            Admin admin = null;
            while (rs.next()) {
                admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setName(rs.getString("name"));
                admin.setPassword(rs.getString("password"));
                admin.setRealName(rs.getString("realname"));
                list.add(admin);
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return list;
    }

    @Override
    public int findAllCount() {
             Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count=0;
        try {
            con = MySQLHelper.connect();
            String sql = "select count(*) as count from admin";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
               count=rs.getInt("count");
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return count;
    }
}
